package com.example.forum.controller;

import com.example.forum.common.AppResult;
import com.example.forum.common.ResultCode;
import com.example.forum.model.Article;
import com.example.forum.model.ArticleReply;
import com.example.forum.service.impl.ArticleReplyServiceImpl;
import com.example.forum.utils.JWTUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.NonNull;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

@RestController
@Api("帖子回复接口")
@RequestMapping("/reply")
public class ArticleReplyController {

    @Autowired
    private ArticleReplyServiceImpl replyService;
    @ApiOperation("添加帖子回复")
    @RequestMapping("/create")
    public AppResult create(@RequestParam("article_id") @ApiParam("文章id") @NonNull Long id,
                            @RequestParam("content") @ApiParam("帖子回复内容") @NonNull String content,
                            HttpServletRequest request) {

        String stringUserId = JWTUtils.getUserId(request.getHeader("user_token"));
        Long userId = Long.parseLong(stringUserId);
        Article article = replyService.selectByArticleId(id);
        if(article.getUser_id().equals(userId)) {
            return AppResult.fail("当前状态不能回复");
        }
        Integer ret = replyService.createArticleReply(id,content,userId);
        if(ret == null || ret <= 0) {
            return AppResult.fail("新增回复帖子失败");
        }
        return AppResult.success("新增回复帖子成功");

    }

    @ApiOperation("获取回复列表")
    @RequestMapping("/getReplies")
    public AppResult getReplies(@RequestParam("articleId") @ApiParam("文章id") @NonNull Long articleId) {

        List<ArticleReply> replies = replyService.getAllArticleReply(articleId);
        return AppResult.success(replies);
    }
}
